﻿using System.ComponentModel;
using System.Web.UI;

namespace OF.Controls
{
    /// <summary>
    /// 文件名称：下拉框控件
    /// 如果使用本控件作为搜索控件,在所有有效项所加入的类似--全部--的项对应的value值应为空字符串或者不设置
    /// </summary>
    [ToolboxData("<{0}:OFDropDownList runat=server></{0}:OFDropDownList>")]
    public class OFDropDownList : System.Web.UI.WebControls.DropDownList, ISearcher
    {
        #region sql搜索条件接口的实现
        /// <summary>
        /// 获取或设置sql条件字段
        /// </summary>
        [Bindable(false), Category("Behavior"), DefaultValue(""), Description("获取或设置sql条件字段。")]
        public string SqlField
        {
            get
            {
                object o = ViewState["SqlField"];
                return (o == null) ? "" : o.ToString();
            }
            set
            {
                ViewState["SqlField"] = value;
            }
        }
        /// <summary>
        /// 获取或设置sql条件关系符
        /// </summary>
        [Bindable(false), Category("Behavior"), DefaultValue("等于"), TypeConverter(typeof(CharacterTypeConverter)), Description("获取或设置sql条件关系符。")]
        public string SqlCharacter
        {
            get
            {
                object o = ViewState["SqlCharacter"];
                return (o == null) ? "" : o.ToString();
            }
            set
            {
                ViewState["SqlCharacter"] = value;
            }
        }
        /// <summary>
        /// 获取搜索条件语句
        /// </summary>
        /// <returns></returns>
        public string GetSqlWhere()
        {
            if (SelectedIndex >= 0 && !string.IsNullOrWhiteSpace(SelectedValue))
            {
                string txt = this.SelectedValue.Trim();
                string strWhere;
                switch (SqlCharacter)
                {
                    case "小于":
                        strWhere = SqlField + " < '" + txt + "'";
                        break;
                    case "等于":
                        strWhere = SqlField + " = '" + txt + "'";
                        break;
                    case "大于":
                        strWhere = SqlField + " > '" + txt + "'";
                        break;
                    case "小于等于":
                        strWhere = SqlField + " <= '" + txt + "'";
                        break;
                    case "大于等于":
                        strWhere = SqlField + " >= '" + txt + "'";
                        break;
                    case "不等于":
                        strWhere = SqlField + " <> '" + txt + "'";
                        break;
                    case "包含":
                        strWhere = SqlField + " like '%" + txt + "%'";
                        break;
                    case "开始":
                        strWhere = SqlField + " like '" + txt + "%'";
                        break;
                    case "结束":
                        strWhere = SqlField + " like '%" + txt + "'";
                        break;
                    case "存在于":
                        strWhere = SqlField + " in (" + txt + ")";
                        break;
                    case "空值":
                        strWhere = SqlField + " is " + txt;
                        break;
                    default:
                        strWhere = SqlField + " = '" + txt + "'";
                        break;
                }

                return strWhere;
            }
            return "";
        }

        /// <summary>
        /// 获取当前控件的ID
        /// </summary>
        /// <returns></returns>
        public string GetControlId()
        {
            return this.ID;
        }
        /// <summary>
        /// 获取或设置搜索分组
        /// </summary>
        [Bindable(false), Category("Behavior"), DefaultValue(""), Description("获取或设置sql搜索分组。")]
        public string SearcherGroupName
        {
            get
            {
                object o = ViewState["SearcherGroupName"];
                return (o == null) ? "" : o.ToString();
            }
            set
            {
                ViewState["SearcherGroupName"] = value;
            }
        }
        #endregion
    }
}
